﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Tab : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Command { get; set; }
        public string CssClass { get; set; }
        public string Description { get; set; }
        public string Id { get; set; }
        public int? sequence { get; set; }
        public string Title { get; set; }
        public Scaling Scaling { get; set; }
        public Groups Groups { get; set; }

        public Tab()
        {
            this.Scaling = new Scaling();
            this.Groups = new Groups();
        }

        public Tab(XmlNode node):this()
        {
            try
            {

                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.CssClass = XmlHelper.GetAttribute(node, "CssClass");
                this.Description = XmlHelper.GetAttribute(node, "Description");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.sequence = XmlHelper.GetNullableIntAttribute(node, "sequence");
                this.Title = XmlHelper.GetAttribute(node, "Title");

                if (node.SelectSingleNode("Scaling") != null)
                {
                    this.Scaling = new Scaling(node.SelectSingleNode("Scaling"));
                }

                if (node.SelectSingleNode("Groups") != null)
                {
                    this.Groups = new Groups(node.SelectSingleNode("Groups"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode tabXmlNode = XmlHelper.AddNode(commandUINode, "Tab");
                XmlHelper.AddAttribute(tabXmlNode, "Command", this.Command);
                XmlHelper.AddAttribute(tabXmlNode, "CssClass", this.CssClass);
                XmlHelper.AddAttribute(tabXmlNode, "Description", this.Description);
                XmlHelper.AddAttribute(tabXmlNode, "Id", this.Id);
                XmlHelper.AddAttribute(tabXmlNode, "sequence", this.sequence);
                XmlHelper.AddAttribute(tabXmlNode, "Title", this.Title);

                if (this.Scaling != null)
                {
                    this.Scaling.AddNode(tabXmlNode);
                }

                if (this.Groups != null)
                {
                    this.Groups.AddNode(tabXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Tab");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Scaling", "Groups" }, true);

                if (this.Scaling != null)
                {
                    node.ContextMenuStrip.Items[0].Enabled = false;
                    this.Scaling.BuildTreeViewElement(node);
                }

                if (this.Groups != null)
                {
                    node.ContextMenuStrip.Items[1].Enabled = false;
                    this.Groups.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Scaling) && !(child is Groups))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (child is Scaling)
                    {
                        this.Scaling = (Scaling)child;
                    }
                    else
                    {
                        this.Groups = (Groups)child;
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Scaling)
                {
                    this.Scaling = null;
                }
                else if (o is Groups)
                {
                    this.Groups = null;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}